package com.huluobo.hyj.hyjmodel.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.adapter.ContactListAdapter
import com.huluobo.hyj.hyjmodel.adapter.EMContactListenerAdapter
import com.huluobo.hyj.hyjmodel.contract.contract.ContractContact
import com.huluobo.hyj.hyjmodel.contract.contract.ContractPresenter
import com.huluobo.hyj.hyjmodel.ui.activity.AddFristActivity
import com.huluobo.hyj.hyjmodel.widet.SlideBar
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class ContractFragment : BaseFragment() ,ContractContact.View{

    val presenter  = ContractPresenter(this)

    override fun getLayoutResId(): Int = R.layout.fragment_contacts
    //设置监听器
    private val contactListener = object : EMContactListenerAdapter(){

        override fun onContactDeleted(username: String?) {
            //重新获取联系人
            presenter.loadContacts()
        }

        override fun onContactAdded(username: String?) {
            super.onContactAdded(username)//添加好友时重新获取一次数据
            //重新获取联系人
            presenter.loadContacts()
        }
    }

    override fun init() {
        super.init()

        //添加头
        initHeader()
        initSipeRefreshLayout()
        initRecyclerView()
        EMClient.getInstance().contactManager().setContactListener(contactListener)
        initSlideBar()


        //进入页面时需要联系人
        presenter.loadContacts()

          //旧写法
//        swipeRefreshLayout.setColorSchemeResources(R.color.qq_blue)
//        swipeRefreshLayout.isRefreshing = true
        //Kotlin的高级语法，新写法
    }

    private fun initSlideBar() {
        slideBar.onSectionChangeListener = object : SlideBar.OnSectionChangeListener{
            override fun onSectionChange(firstLetter: String) {
                //按住就显示一个logo
                section.visibility = View.VISIBLE
                section.text = firstLetter
                //平滑滑动
                recyclerView.smoothScrollToPosition(getPosition(firstLetter))
            }

            override fun onSlidFinish() {
                //松手就消失
                section.visibility = View.GONE
            }
        }
    }

    private fun initRecyclerView() {
        recyclerView.apply {
            setHasFixedSize(true)//可以修改长度，当Recylerview长度发生变化时，内部会做优化处理
            layoutManager = LinearLayoutManager(context) //显示方法
            adapter = ContactListAdapter(context,presenter.contactListItems) //创建适配器和传参
        }

//        //设置好友动态监听
//        EMClient.getInstance().contactManager().setContactListener(object :
//            EMContactListenerAdapter() {
//        })
    }

    private fun initSipeRefreshLayout() {
        swipeRefreshLayout.apply {
            //当前控件或者类的床架只能和模式
            setColorSchemeResources(R.color.qq_blue)
            isRefreshing = true
            //上拉下拉需要初始化诗句
            setOnRefreshListener {
                presenter.loadContacts()
            }
        }
    }

    private fun initHeader() {
        headerTitle.text = getString(R.string.contact)

        add.visibility = View.VISIBLE

        //添加好友页面 跳转到添加页面
        add.setOnClickListener {
            context?.startActivity<AddFristActivity>()
        }
    }

    //适配器的滑动
    private fun getPosition(firstLetter: String): Int =
        //binarySearch就是二分查找的方法api自带的
        presenter.contactListItems.binarySearch {
            //通过首字母找到具体的key，再通过key去滑动RecyclerView
            contactListItem ->   contactListItem.firstLetter.minus(firstLetter[0])
        }


    //成功
    override fun onLoadContactSuccess() {
        //加载成功时隐藏进度条
        swipeRefreshLayout.isRefreshing = false
        //刷新适配器
        recyclerView.adapter?.notifyDataSetChanged()
    }
    //失败
    override fun onLoadContactFailed() {
        swipeRefreshLayout.isRefreshing = false
        context?.toast(R.string.load_contacts_failed)
    }
}